package com.airhockey;

import com.airhockey.R;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

/**
 * Option activity allow to modify some preferences about the application 
 * like server adress , port , or login of the user
 * @author Maxime
 */

public class OptionActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_option);		
		SetFields();
	}

	/**
	 * Fill the form with informations
	 */
	public void SetFields()
	{
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		EditText pseudo = (EditText) findViewById(R.id.editTextPseudo);
		pseudo.setText(sharedPreferences.getString(AppData.pseudoKey,AppData.pseudoDefault));
		
		EditText server = (EditText) findViewById(R.id.editTextServer);
		server.setText(sharedPreferences.getString(AppData.ipServerKey,AppData.ipServerDefault));
		
		EditText port = (EditText) findViewById(R.id.editTextPort);
		port.setText(String.valueOf(sharedPreferences.getInt(AppData.portKey, AppData.portDefault)));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.option, menu);
		return true;
	}
	
	/**
	 * Validate the form
	 * @param view
	 */
	public void Validate(View view)
	{
		Intent i = new Intent();

		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		Editor editor = sharedPreferences.edit();
		
		EditText pseudo = (EditText) findViewById(R.id.editTextPseudo);
		editor.putString(AppData.pseudoKey, pseudo.getText().toString());
		
		EditText server = (EditText) findViewById(R.id.editTextServer);
		editor.putString(AppData.ipServerKey, server.getText().toString());
		
		EditText port = (EditText) findViewById(R.id.editTextPort);
		editor.putInt(AppData.portKey,Integer.valueOf(port.getText().toString()));
		
		editor.commit();
		
		setResult(RESULT_OK, i);
		
		AppData.soundPool.play(AppData.soundButton, 1, 1, 0, 0, 1);
		
		finish();
	}
	
	/**
	 * Empty all the preferences
	 * @param view
	 */
	public void Reset(View view)
	{
		AppData.soundPool.play(AppData.soundButton, 1, 1, 0, 0, 1);
		final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		final AlertDialog ad = new AlertDialog.Builder(this).create();  

		ad.setTitle(R.string.sure);  
		   ad.setButton(DialogInterface.BUTTON_POSITIVE,getApplicationContext().getString(R.string.OK), 
	        		 new DialogInterface.OnClickListener() {  
	             @Override  
	             public void onClick(DialogInterface dialog, int which) {  

	            	 Editor editor = sharedPreferences.edit();
	            	 editor.clear();
	            	 editor.commit();	            	 
	            	 SetFields();
	             }  
		   });   
		   
		ad.setButton(DialogInterface.BUTTON_NEGATIVE, 
				getApplicationContext().getString(R.string.cancel), 
				new DialogInterface.OnClickListener() {  
			@Override  
			public void onClick(DialogInterface dialog, int which) 
			{    
				ad.dismiss();

			}  
		});  
		ad.show(); 

	}
	
	/**
	 * Cancel all
	 * @param view
	 */
	public void Cancel(View view)
	{
		setResult(RESULT_CANCELED, new Intent());	
		AppData.soundPool.play(AppData.soundButton, 1, 1, 0, 0, 1);	
		finish();
	}

}
